package com.engine4cloud.tdf.app.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.engine4cloud.tdf.app.api.dto.ClassifySortDTO;
import com.engine4cloud.tdf.app.api.entity.AppClassifyEntity;
import com.engine4cloud.tdf.app.service.AppClassifyService;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @description 分类表控制器
* @author BEJSON
* @date 2024-09-06
*/
@Slf4j
@RestController
@RequestMapping("/appClassify")
public class AppClassifyController {

    @Autowired
    private AppClassifyService appClassifyService;

    /**
    * 新增或编辑
    */
    @Operation(summary = "分类表", description = "分类表")
    @SysLog("分类表")
    @PostMapping
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_add')")
    public R save(@RequestBody AppClassifyEntity appClassify) {
        return R.ok(appClassifyService.save(appClassify));
    }

    /**
    * 删除
    */
    @Operation(summary = "通过id删除分类表", description = "通过id删除分类表")
    @SysLog("通过id删除分类表")
    @DeleteMapping
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(appClassifyService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
    * 查询
    */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_view')")
    public R getAppClassifyPage(@ParameterObject Page page,
                                       @ParameterObject AppClassifyEntity appClassify) {
        LambdaQueryWrapper<AppClassifyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(appClassify.getDeviceType() >= 0, AppClassifyEntity::getDeviceType, appClassify.getDeviceType());
        wrapper.orderByAsc(AppClassifyEntity::getClassifySort);
        return R.ok(appClassifyService.page(page, wrapper));
    }

    /**
     * 通过id查询分类表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(appClassifyService.getById(id));
    }


    /**
     * 修改文章分类表
     *
     * @param appClassify 分类表
     * @return R
     */
    @Operation(summary = "修改分类表", description = "修改分类表")
    @SysLog("修改分类表")
    @PutMapping
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_edit')")
    public R updateById(@RequestBody AppClassifyEntity appClassify) {
        return R.ok(appClassifyService.updateById(appClassify));
    }

    /**
     * 修改排序
     *
     * @param appClassifys
     * @return R
     */
    @SysLog("修改排序")
    @PostMapping("/updateSort")
    @Inner(value = false)
//    @PreAuthorize("@pms.hasPermission('app_classify_edit')")
    public R updateSortById(@RequestBody List<AppClassifyEntity> appClassifys) {
        return R.ok(appClassifyService.updateBatchById(appClassifys));
    }

}